package com.hele.sts.action.admin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.hele.sts.dao.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hele.sts.pub.StringUtil;
import com.hele.sts.webwork.PageAction;
import com.opensymphony.xwork2.ActionContext;
import org.springframework.beans.factory.annotation.Autowired;

public class DocumentManagerAction
  extends PageAction
{

  @Autowired
  private XtLogDao xtLogDao;

  @Override
  public String execute()
    throws Exception
  {
    ActionContext ctx = ActionContext.getContext();
    Map session = ctx.getSession();
    String loginName = (String)session.get("AdminUser.loginName");
    if ((loginName == null) || (loginName.length() < 1))
    {
      setErrorInfo("没有登录!");
      return "error";
    }
    if ("borrow".equalsIgnoreCase(this.action)) {
      return borrow();
    }
    if ("return".equalsIgnoreCase(this.action)) {
      return ret();
    }
    if ("listall".equalsIgnoreCase(this.action)) {
      return listAll();
    }
    if ("dispatch".equalsIgnoreCase(this.action))
    {
      logger.debug(this.action + "-----");
      return dispatch();
    }
    return super.execute();
  }
  
  public String dispatch()
  {
    logger.debug(this.userId + "--------");
    this.qEkUser = QEkUserDao.load(this.userId);
    this.cxqxList = BMCxqxDao.getCxqxList();
    Collections.reverse(cxqxList);
    if (this.qEkUser == null) {
      return "error";
    }
    return "dispatch";
  }
  
  @Override
  public String getHref()
  {
    String href = "";
    if ((this.userId != null) && (this.userId.length() > 0)) {
      href = href + "&userId=" + this.userId;
    }
    if ((this.qymc != null) && (this.qymc.length() > 0)) {
      href = href + "&qymc=" + this.qymc;
    }
    if ((this.nbxh != null) && (this.nbxh.length() > 0)) {
      href = href + "&nbxh=" + this.nbxh;
    }
    if ((this.zch != null) && (this.zch.length() > 0)) {
      href = href + "&zch=" + this.zch;
    }
    return href;
  }
  
  public String borrow()
  {
    ActionContext ctx = ActionContext.getContext();
    Map session = ctx.getSession();
    List list = (List)session.get("list");
    logger.debug(this.userId);
    this.qEkUser = QEkUserDao.load(this.userId);
    if (this.qEkUser == null)
    {
      setErrorInfo("请先关联用户!");
      return "error";
    }
    boolean b = true;
    if ((list != null) && (list.size() > 0)) {
      for (Iterator iterator = list.iterator(); iterator.hasNext();)
      {
        QEnterQueue q = (QEnterQueue)iterator.next();
        logger.debug(q.getNbxh());
        b = QEnterQueueDao.delete(this.userId, q.getNbxh());
        if (!b)
        {
          setErrorInfo("删除失败!");
          return "error";
        }
      }
    }
    for (int i = 0; (this.nbxhs != null) && (i < this.nbxhs.length); i++)
    {
      this.qyhznr = QyhznrDao.load(this.nbxhs[i]);
      if (this.qyhznr == null) {
          this.qyhznr = QyhznrDao.loadBfTj(this.nbxhs[i]);
        }
      b = (b) && (QEnterQueueDao.insert(this.userId, this.qyhznr.getNbxh(), this.qyhznr.getZch(), this.qyhznr.getQymc(),this.qyhznr.getDjjg(),this.qyhznr.getQylx()));
    }
    if (!b)
    {
      setErrorInfo("添加档案数据失败!");
      return "error";
    }
    session.remove("list");
    return "borrow";
  }
  
  public String ret()
  {
    QEkUser bean = QEkUserDao.load(this.userId);
    this.qEnterQueueList = QEnterQueueDao.load(this.userId);
    if (bean != null)
    {
      QUser quser = QUserDao.load(this.userId);
      if ((this.qEnterQueueList != null) && (this.qEnterQueueList.size() > 0))
      {
        for (Iterator iterator = this.qEnterQueueList.iterator(); iterator.hasNext();)
        {
          QEnterQueue qBean = (QEnterQueue)iterator.next();
          if ((qBean != null) && (quser != null))
          {
            QLog qLog = createrQLog(qBean, quser, bean);
            if (!QLogDao.insert(qLog))
            {
              setErrorInfo("保存日志失败!");
              return "error";
            }
          }
        }
        if (!QEnterQueueDao.delete(this.userId))
        {
          setErrorInfo("删除档案数据失败!");
          return "error";
        }
      }
      quser.setUserName("");
      quser.setCxqx("");
      quser.setDyqx("");
      quser.setStatus("1");
      quser.setGhrq(new Date());
      if (!QUserDao.update(quser))
      {
        setErrorInfo("更新用户失败!");
        return "error";
      }
    }
    return "ret";
  }
  
  public String listAll()
  {
    ActionContext ctx = ActionContext.getContext();
    Map session = ctx.getSession();
    User adminUser = (User)session.get(("AdminUser"));
    logger.debug(this.userId + "------------");
    xtLogDao.insertLog("业务日志","后管查看用户信息",adminUser.getUserId(),adminUser.getUserName(),"成功","用户："+adminUser.getUserId()+" 查询用户："+userId+" 信息",this.getClass().getName());


    this.qEkUser = QEkUserDao.load(this.userId);
    this.qEnterQueueList = QEnterQueueDao.load(this.userId);
    if ((this.qEkUser == null) && (this.qEnterQueueList.size() > 0))
    {
      logger.debug("why" + this.qEnterQueueList);
      for (Iterator iterator = this.qEnterQueueList.iterator(); iterator.hasNext();)
      {
        QEnterQueue qBean = (QEnterQueue)iterator.next();
        QUser quser = QUserDao.load(this.userId);
        if ((qBean != null) && (quser != null))
        {
          QLog qLog = createrQLog(qBean, quser, this.qEkUser);
          if (!QLogDao.insert(qLog))
          {
            setErrorInfo("保存日志失败!");
            return "error";
          }
        }
      }
      if (!QEnterQueueDao.delete(this.userId))
      {
        setErrorInfo("删除档案数据失败!");
        return "error";
      }
    }
    return "listall";
  }
  
  @Override
  public int count()
  {
    if (this.noList) {
      return 0;
    }
    return QyhznrDao.count(this.qymc, this.nbxh, this.zch,null,null);
  }
  
  @Override
  public String list()
  {
    if (this.noList) {
      return "list";
    }
    if (this.userId != null) {
      this.qEnterQueueList = QEnterQueueDao.load(this.userId);
    }
    this.qyhznrList = QyhznrDao.load(this.qymc, this.nbxh, this.zch,null,null, getPageNo(), getPageSize());
    if (this.qEnterQueueList.size() > 0)
    {
      logger.debug(this.qEnterQueueList.size() + "size");
      Iterator iterator = this.qEnterQueueList.iterator();
      List list = new ArrayList();
      QEnterQueue q = null;
      Qyhznr y = null;
      int i = 0;
      //TODO 修正
//      for (; iterator.hasNext() && i < qyhznrList.size() ;)
//      {
//        q = (QEnterQueue)iterator.next();
//        logger.debug(q.getNbxh());
//        y = (Qyhznr)this.qyhznrList.get(i);
//        logger.debug(y.getNbxh());
//        if (q.getNbxh().equals(y.getNbxh())) {
//          list.add(q);
//        }
//      }
      
		for (;iterator.hasNext();) {
			q = (QEnterQueue)iterator.next();
			for (int j = 0; j < qyhznrList.size(); j++) {
		    	  y = (Qyhznr)this.qyhznrList.get(j);
		    	  if (q.getNbxh().equals(y.getNbxh())) {
		    		  list.add(q);
		    		  break;
		    	  }
		}
	}
      logger.debug(list.size() + "l");
      ActionContext ctx = ActionContext.getContext();
      Map session = ctx.getSession();
      session.put("list", list);
    }
    return "list";
  }
  
  QEkUser createQEkUser()
  {
    QEkUser bean = new QEkUser();
    bean.setBlance(this.blance);
    bean.setCertificate(this.certificate);
    bean.setDepartment(this.department);
    bean.setDiscount(this.discount);
    bean.setIpAddress(this.ipAddress);
    bean.setPhysicalAddress(this.physicalAddress);
    bean.setTelephone(this.telephone);
    bean.setUnit(this.unit);
    bean.setUserId(this.userId);
    bean.setUserName(this.userName);
    return bean;
  }
  
  QLog createrQLog(QEnterQueue eBean, QUser qUser, QEkUser bean)
  {
    QLog log = new QLog();
    logger.debug(qUser.getCxrq() + "." + StringUtil.createLogId() + "." + eBean.getNbxh() + "." + eBean.getQymc().trim() + "." + eBean.getUserId() + "." + qUser.getYhlx());
    logger.debug(qUser.getUserName() + "." + bean.getUserName() + "." + eBean.getZch());
    log.setCxrq(qUser.getCxrq());
    log.setLogId(StringUtil.createLogId());
    log.setNbxh(eBean.getNbxh().trim());
    log.setQymc(eBean.getQymc().trim());
    log.setUserId(eBean.getUserId().trim());
    log.setUserName(bean.getUserName().trim());
    log.setUserNameEk(qUser.getUserName().trim());
    log.setYhlx(qUser.getYhlx().trim());
    log.setZch(eBean.getZch().trim());
    return log;
  }
  
  public String getAction()
  {
    return this.action;
  }
  
  public void setAction(String action)
  {
    this.action = action;
  }
  
  public QEkUser getQEkUser()
  {
    return this.qEkUser;
  }
  
  public void setQEkUser(QEkUser qEkUser)
  {
    this.qEkUser = qEkUser;
  }
  
  public List getQEnterQueueList()
  {
    return this.qEnterQueueList;
  }
  
  public void setQEnterQueueList(List qEnterQueueList)
  {
    this.qEnterQueueList = qEnterQueueList;
  }
  
  public String getUserId()
  {
    return this.userId;
  }
  
  public void setUserId(String userId)
  {
    this.userId = userId;
  }
  
  public String getErrorInfo()
  {
    return this.errorInfo;
  }
  
  public void setErrorInfo(String errorInfo)
  {
    this.errorInfo = errorInfo;
  }
  
  public String getUserName()
  {
    return this.userName;
  }
  
  public void setUserName(String userName)
  {
    this.userName = userName;
  }
  
  public String getUnit()
  {
    return this.unit;
  }
  
  public void setUnit(String unit)
  {
    this.unit = unit;
  }
  
  public String getDepartment()
  {
    return this.department;
  }
  
  public void setDepartment(String department)
  {
    this.department = department;
  }
  
  public String getCertificate()
  {
    return this.certificate;
  }
  
  public void setCertificate(String certificate)
  {
    this.certificate = certificate;
  }
  
  public String getTelephone()
  {
    return this.telephone;
  }
  
  public void setTelephone(String telephone)
  {
    this.telephone = telephone;
  }
  
  public String getPhysicalAddress()
  {
    return this.physicalAddress;
  }
  
  public void setPhysicalAddress(String physicalAddress)
  {
    this.physicalAddress = physicalAddress;
  }
  
  public String getIpAddress()
  {
    return this.ipAddress;
  }
  
  public void setIpAddress(String ipAddress)
  {
    this.ipAddress = ipAddress;
  }
  
  public double getDiscount()
  {
    return this.discount;
  }
  
  public void setDiscount(double discount)
  {
    this.discount = discount;
  }
  
  public double getBlance()
  {
    return this.blance;
  }
  
  public void setBlance(double blance)
  {
    this.blance = blance;
  }
  
  public String[] getNbxhs()
  {
    return this.nbxhs;
  }
  
  public void setNbxhs(String[] nbxhs)
  {
    this.nbxhs = nbxhs;
  }
  
  public String getQymc()
  {
    return this.qymc;
  }
  
  public void setQymc(String qymc)
  {
    this.qymc = qymc;
  }
  
  public String getNbxh()
  {
    return this.nbxh;
  }
  
  public void setNbxh(String nbxh)
  {
    this.nbxh = nbxh;
  }
  
  public String getZch()
  {
    return this.zch;
  }
  
  public void setZch(String zch)
  {
    this.zch = zch;
  }
  
  public List getQyhznrList()
  {
    return this.qyhznrList;
  }
  
  public void setQyhznrList(List qyhznrList)
  {
    this.qyhznrList = qyhznrList;
  }
  
  public String getFlag()
  {
    return this.flag;
  }
  
  public void setFlag(String flag)
  {
    this.flag = flag;
  }
  
  public boolean getNoList()
  {
    return this.noList;
  }
  
  public void setNoList(boolean noList)
  {
    this.noList = noList;
  }
  
  static Logger logger = LoggerFactory.getLogger(DocumentManagerAction.class);
  public static final String BORROW = "borrow";
  public static final String RETURN = "return";
  public static final String LIST = "list";
  public static final String QUERY = "query";
  public static final String LISTALL = "listall";
  public static final String DISPATCH = "dispatch";
  public static final String UPDATE = "update";
  String action;
  QEkUser qEkUser;
  List qEnterQueueList;
  List qyhznrList;
  String userId;
  String errorInfo;
  String userName;
  String unit;
  String department;
  String certificate;
  String telephone;
  String physicalAddress;
  String ipAddress;
  double discount;
  double blance;
  String qymc;
  String nbxh;
  String zch;
  String[] nbxhs;
  Qyhznr qyhznr;
  String flag;
  boolean noList;
  List cxqxList;
/**
 * @return the cxqxList
 */
public List getCxqxList() {
	return cxqxList;
}

/**
 * @param cxqxList the cxqxList to set
 */
public void setCxqxList(List cxqxList) {
	this.cxqxList = cxqxList;
}
}
